<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/18
 * Time: 14:43
 */

namespace core;


use Predis\Client;

class Redis
{
    /**
     * @var self
     */
    private static $instance;
    /**
     * @var Client
     */
    protected $redis;

    public function __construct()
    {
        try{
            $redis = new \Redis();
            $redisConfig = Config::getInstance()->getConfig('redis');
            if (!$redis->connect($redisConfig['host'],$redisConfig['port'])){
                throw new \Exception('无法连接上Redis');
            }
            $redis->select($redisConfig['database']);
            $this->redis = $redis;
        }catch (\Exception $exception){
            $this->redis = new Client(Config::getInstance()->getConfig('redis'));
        }
    }

    /**
     * @return Client|\Redis
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)){
            self::$instance = new static();
        }
        return self::$instance->redis;
    }
}